/**
 * 
 */
package org.mspring.mlog.web.module.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.security.Resource;
import org.mspring.mlog.service.security.ResourceService;
import org.mspring.mlog.support.log.Log;
import org.mspring.mlog.web.query.ResourceQueryCriterion;
import org.mspring.mlog.web.security.SecurityUtils;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.utils.SessionUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Gao Youbo
 * @since 2013-6-9
 * @description
 * @TODO
 */
@Widget
@RequestMapping("/admin/resource")
@SuppressWarnings("rawtypes")
public class Admin_ResourceWidget extends Admin_AbstractWidget {
    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/list")
    @Log
    public String list(@ModelAttribute Page<Resource> resourcePage, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (resourcePage == null) {
            resourcePage = new Page<Resource>();
        }
        if (resourcePage.getSort() == null) {
            resourcePage.setSort(new Sort("id", Sort.DESC));
        }
        resourcePage = resourceService.findResources(new ResourceQueryCriterion(queryParams), resourcePage);
        model.addAttribute("resourcePage", resourcePage);
        return "/admin/resource/listResource";
    }

    @RequestMapping("/create")
    public String create(@ModelAttribute Resource resource, HttpServletRequest request, HttpServletResponse response, Model model) {
        return "/admin/resource/createResource";
    }

    @RequestMapping("/create/save")
    @Log
    public String create_save(@ModelAttribute Resource resource, HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            resourceService.createResource(resource);

            // 重新加载资源-权限对应关系
            SecurityUtils.reloadResourceDefine();
            return prompt(model, "", "资源增加成功", "/admin/resource/list");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return prompt(model, "资源增加失败");
        }
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam(required = false) Long id, @ModelAttribute Resource resource, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            id = SessionUtils.getAttrAsLong(request, "Admin_ResourceWidget_edit_id");
        }
        if (id == null) {
            return prompt(model, "请选择要修改的资源");
        }

        resource = resourceService.getResourceById(id);
        if (resource == null) {
            return prompt(model, "请选择要修改的资源");
        }
        model.addAttribute("resource", resource);
        SessionUtils.setAttr(request, "Admin_ResourceWidget_edit_id", id);
        return "/admin/resource/editResource";
    }

    @RequestMapping("/edit/save")
    @Log
    public String edit_save(@ModelAttribute Resource resource, HttpServletRequest request, HttpServletResponse response, Model model) {
        boolean flag = resourceService.updateResource(resource);
        if (flag) {
            // 重新加载资源-权限对应关系
            SecurityUtils.reloadResourceDefine();
            return prompt(model, "资源修改成功", "/admin/resource/edit?id=" + resource.getId());
        }
        return prompt(model, "资源修改失败");
    }

    @RequestMapping("/delete")
    @Log
    public String delete(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        boolean flag = resourceService.deleteResource(id);
        SecurityUtils.reloadResourceDefine();
        if (flag) {
            SecurityUtils.reloadResourceDefine();
            return prompt(model, "资源删除成功", "/admin/resource/list");
        }
        return prompt(model, "资源删除失败", "/admin/resource/list");
    }

}
